package com.aaa.jur.controller;


import com.aaa.common.entity.*;
import com.aaa.jur.service.MenuService;
import com.aaa.jur.service.RoleMenuService;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 菜单权限表 前端控制器
 * </p>
 *
 * @author AAATeacherChen
 * @since 2020-06-14
 */
@Controller
@RequestMapping("/menu")
public class MenuController {
    @Autowired
    private MenuService menuService;

    @Autowired
    private RoleMenuService roleMenuService;

    // List<LayUiTree> layUiTreeList = menuService.findMenus(username);

    @RequestMapping("/findAllMenus")
    @ResponseBody
    /**
     * description: 查询所有的权限菜单
     * create time: 2020/6/17 16:54
     * @Param:
     * @return java.util.List<com.aaa.entity.LayUiTree>
     */
    public List<LayUiTree> findAllMenus(){
        List<LayUiTree> layUiTreeList = menuService.findMenus(null);
        return layUiTreeList;
    }
    @RequestMapping("/selectMenu")
    @ResponseBody
    public LayUiTable selectMenu(){
        List list = menuService.selectList(null);
        LayUiTable layUiTable = new LayUiTable();
        layUiTable.setCode(0);
        layUiTable.setMsg("成功");
        layUiTable.setCount(0);
        layUiTable.setData(list);
        return layUiTable;
    }
    @RequestMapping("/menu")
    public  String menuList(){
        return "menu";
    }

    @RequestMapping("/selectAllMenus")
    @ResponseBody
    public LayUiTable selectAllMenus(){
        List<Menu> list = menuService.selectList(null);
        LayUiTable layUiTable = new LayUiTable();
        layUiTable.setCode(0);
        layUiTable.setMsg("成功");
        layUiTable.setCount(0);
        layUiTable.setData(list);
        return layUiTable;
    }

    @ResponseBody
    @RequestMapping("/menuName")
    public ResultData menuName(){
        List<Menu> menus = menuService.selectMenuName();
        System.out.println(menus);
        return new ResultData(true,"成功",menus);
    }
    @ResponseBody
    @RequestMapping("/insertMenu")
    public Map insertMenu(Menu menu){
        Map map = new HashMap();
        boolean b = menuService.insert(menu);
        if (b){
            map.put("code",0);
            map.put("msg","保存成功");
        }else {
            map.put("code",1);
            map.put("msg","保存失败");
        }
        return map;
    }
    @ResponseBody
    @RequestMapping("/updateMenu")
    public Map<String, Object> updateMenu(Menu menu) {
        Map<String,Object> map = new HashMap<>();
        if (menu.getVisible() == null){
            menu.setVisible("1");
        }
        boolean update = menuService.updateById(menu);
        if (update){
            map.put("code",0);
            map.put("msg","修改成功");
        }else {
            map.put("code",1);
            map.put("msg","修改失败");
        }
        return map;
    }
    @ResponseBody
    @RequestMapping("selectByParentId")
    public LayUiTable selectByParentId(Integer menuId){
        LayUiTable table = new LayUiTable();
        List<Menu> menus = menuService.selectByParentId(menuId);
        System.out.println(menus);
        if(menus != null&&menus.size()>0){
            table.setCode(1);
            table.setMsg("请先删除目录下面的子菜单");
        }else{
            menuService.deleteById(menuId);
            Wrapper<RoleMenu> wrapper = new EntityWrapper<>();
            wrapper.eq("menu_id",menuId);
            roleMenuService.delete(wrapper);
            table.setCode(0);
            table.setMsg("删除成功");
        }
        return table;
    }

}

